package com.suncnpap.system.controller;

import com.suncnpap.system.model.Config;
import com.suncnpap.system.model.GroupType;
import com.suncnpap.system.model.School;
import com.suncnpap.system.service.ConfigService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.app.util.beanutil.BeanUtil;
import com.app.util.common.Constants;
import com.app.util.common.EasyuiGrid;
import com.app.util.common.HandlerResult;
import com.app.util.common.ResponseUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;

/** 
 * 说明：系统设置Controller
 */
@Controller
@RequestMapping(value="/system/config")
public class ConfigController extends BaseController {
	
	@Autowired
	ConfigService configService;
	
	
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
	
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			page = configService.list(page);
			result = this.returnPage(result, page);
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody Config obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				init(obj);
				configService.add(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
	/**
	 * 修改处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody Config obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				Config config = configService.findBySchoolId(GlobaUser.getStrSchoolId(request.getHeader("token")));
				if(config==null){
					init(obj);
					configService.add(obj);
				}else{
					configService.update(obj);
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					configService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/view")
	public void view(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
//				Page page = this.setPage();
//				page.setRows(1);
//				page.setUsePage(false);
//				page = configService.list(page);
				
//				List<Config> configList = page.getDataList();
				Config config = configService.findBySchoolId(GlobaUser.getStrSchoolId(request.getHeader("token")));
//				if(configList != null && configList.size()>0){
//					config = configList.get(0);
//				}
				result.put("config", config);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 修改处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/updateLocation",method=RequestMethod.POST)
	public void updateLocation(@RequestBody Config obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strCollectionLocation = obj.getStrCollectionLocation();
				String strCollectionLocationId = obj.getStrCollectionLocationId();
				String strId = obj.getStrId();
				obj = configService.findById(strId);
				obj.setStrCollectionLocation(strCollectionLocation);
				obj.setStrCollectionLocationId(strCollectionLocationId);
				configService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
}
